require "csv"

sortedNames = []
names = CSV.read "names.txt"
names.each do |row|
    row.sort!.each do |name|
        sortedNames << name
    end
end

sum = 0
sortedNames.each_with_index do |name, index|
    nameTotal = 0
    name.scan(/./).each do |letter|
        nameTotal += ('A'..'Z').to_a.index(letter) + 1
    end
    sum += nameTotal * (index + 1)
end

puts sum
